import query from '@feidao-factory/atom-nodejs/db/query';

import { systemid } from '../config';
interface Message {
	doccode: string;
}

interface ISalesOrder {
	doccode: string; 	// 	单号doccode
	docstatus: string; // 单据状态docstatus
	cust_name: string; // 客户名称cust_name
	order_money: string;  // 金额order_money
	document_num: number; // 产品总数量document_num
	client_no: string;
	docdate: number;	// 单据日期docdate
	driver_address: string;	// 送货地址driver_address
	arrive_time: number;	// 送货时间arrive_time
	retailer_store_name: string;	// 门店名称retailer_store_name
	salesman: string;	// 业务员名称salesman
	memo: string;	// 备注memo
}
interface IOrderDetail {
	doccode: string; 	// 	单号doccode
	commodityid: string;	// 	产品编号commodityid
	commodity_name: string;	// 产品名称commodity_name
	commodity_model: string;	// 型号commodity_model
	thumbnail: string;	// 产品缩略图thumbnail
	num: string;	// 订单数量num
	unitprice: string;	// 单价unitprice
	deal_qty: number;	// 已处理数量deal_qty
	subtotal: number;	// 小计subtotal
	warranty_information: string;	// 质保信息warranty_information
}

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const p1 = query(action_id, session_id, systemid);
	p1.prepare('sales_order', ['doccode', 'docstatus', 'client_no', 'cust_name', 'order_money', 'document_num', 'docdate', 'driver_address', 'arrive_time', 'retailer_store_name', 'salesman', 'memo', 'tax_rate', 'install_quantity', 'add_cost', 'reduce_amount', 'price_total'], { doccode: msg.doccode }, 1, 1, [], []);


	p1.prepare('order_detail', ['doccode', 'commodityid', 'commodity_name', 'commodity_model', 'thumbnail', 'num', 'unitprice', 'deal_qty', 'subtotal', 'warranty_information'], { doccode: msg.doccode }, 100, 1, [], []);
	const [sales_orders, order_details] = await p1.exec<ISalesOrder, IOrderDetail>();


	if (sales_orders.length === 0 && order_details.length === 0) {
		return;
	}

	const sales_order = sales_orders[0];
	return { ...sales_order, datas: order_details };
}
